﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using System.IO;
using System.Collections;
using ThapCam.System;

/// <summary>
/// Summary description for FOUtility
/// </summary>
namespace ThapCam.Ulti
{
    public static class FOUtility
    {
        public static int convertBooleanToInteger(bool value)
        {
            return value ? 1 : 0;
        }

        public static string formatToCurrency(double price)
        {
            return String.Format("{0:0,0} đ", price);
        }

        public static string formatNumber(double price)
        {
            return String.Format("{0:0,0}", price);
        }

        public static string formatRowNumber(int numRow)
        {
            return String.Format("{0:000}", numRow);
        }

        public static bool convertStringToBoolean(string value)
        {
            value = value.ToLower();
            if (value == "1" || value == "true")
                return true;
            return false;
        }

        public static string convertBooleanToString(bool value)
        {
            return value == true ? "1" : "0";
        }
       
        public static string convertDateToString(DateTime date)
        {
            if (date != null)
                return String.Format("{0:yyyy-MM-dd HH:mm:ss}", date);
            return "";
        }

        public static DateTime convertStringToDateTime(string data)
        {
            return DateTime.Parse(data);
        }

        public static string randomString(int length)
        {
            Random random = new Random(DateTime.Now.Millisecond);
            String randomStr = "";
            int randNumber;
            for (int i = 0; i < length; i++)
            {
                if ((random.Next(1, 100) % 2) == 0)
                    randNumber = random.Next(97, 123); //char {a-z}
                else
                    randNumber = random.Next(48, 58); //int {0-9}

                randomStr = randomStr + (char)randNumber;
            }
            return randomStr;
        }

        public static string randomNumberString(int length)
        {
            Random random = new Random(DateTime.Now.Millisecond);
            String randomStr = "";
            int randNumber;
            for (int i = 0; i < length; i++)
            {               
                randNumber = random.Next(48, 58); //int {0-9}
                randomStr = randomStr + (char)randNumber;
            }
            return randomStr;
        }

        public static string clearUnicode(string accented)
        {
            if (accented == null || accented == "")
                return "";
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = accented.Normalize(NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        public static string optimzeUrl(String url)
        {
            String value = clearUnicode(url);
            if (String.IsNullOrEmpty(value))
                return "";
            else
                return HttpUtility.UrlEncode(Regex.Replace(value, "[^a-zA-Z0-9_]+", "-", RegexOptions.Compiled));

        }
       
        public static string encryptRC4(string clearText, string key)
        {
            string cryptedText = RC4.Encrypt(clearText, key);
            return cryptedText;
        }

        public static string decryptRC4(string cryptedText, string key)
        {
            string clearText = RC4.Decrypt(cryptedText, key);
            return clearText;
        }

        public static bool isEmail(string email)
        {            
            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(email);
            return match.Success;
        }

        public static string optimizeLazyLoadImage(string content, string pathReplace)
        {           
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(content);
           
			HtmlNodeCollection resultSet = doc.DocumentNode.SelectNodes("//*[@src]");
            if (resultSet == null || resultSet.Count == 0)
				return content;
            foreach (HtmlNode node in resultSet)
            {
                if (node.Name == "img")
                {
                    if (node.Attributes["src"] == null)
                    {
                        node.Attributes.Add("scr", pathReplace);
                        continue;
                    }

                    node.Attributes["src"].Value = node.Attributes["src"].Value.Replace("http://admin.foodonline.vn/", WebConfig.HOST_RESOURCE).Replace("http://nhahang.foodonline.vn/", WebConfig.HOST_RESOURCE);
                    
                    if (node.Attributes["data-original"] == null)
                        node.Attributes.Add("data-original", node.Attributes["src"].Value);
                    else
                        node.Attributes["data-original"].Value = node.Attributes["src"].Value;

                    if (node.Attributes["class"] == null)
                        node.Attributes.Add("class", "lazy rounded content-img1");
                    else                                            
                        node.Attributes["class"].Value = "lazy rounded content-img1";                    
                    
                    node.Attributes["src"].Value = pathReplace;

                    if (node.ParentNode != null && node.ParentNode.Name == "a")
                    {
                        if (node.ParentNode.Attributes["href"] == null)
                            node.ParentNode.Attributes.Add("href", "javascript:;");
                        else
                            node.ParentNode.Attributes["href"].Value = "javascript:;";
                        
                        if (node.ParentNode.ParentNode != null && node.ParentNode.ParentNode.Name == "p")
                        {
                            if (node.ParentNode.ParentNode.Attributes["class"] == null)
                                node.ParentNode.ParentNode.Attributes.Add("class", "wrap-img");
                            else                                                            
                                node.ParentNode.ParentNode.Attributes["class"].Value = "wrap-img";                            
                        }
                    }
                }
            }
            return doc.DocumentNode.OuterHtml;           
        }
    }
}